package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;
    @Reference
    private MenuService menuService;
    //获取角色信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = roleService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    //保存角色与权限以及菜单的关联关系
    @RequestMapping("/saveRPM")
    public Result saveRPM(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.saveRPM(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新建失败");
        }
        return new Result(true,"新建成功");
    }

    //删除选中id角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        return roleService.delete(id);
    }

    //回显角色数据
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Result result =new Result(true,"角色数据回显成功");
        try {
            Role role=roleService.findById(id);
            result.setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色数据回显失败");
        }
        return result;
    }

    //查询角色与权限的关联关系
    @RequestMapping("/findRoleAndPermission")
    public Result findRoleAndPermission(Integer id){
        Result result =new Result(true,"关联关系数据回显成功");
        try {
            List<Integer> permissionIds=roleService.getRoleAndPermission(id);
            result.setData(permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"关联关系数据回显失败");
        }
        return result;
    }

    //查询角色与菜单的关联关系
    @RequestMapping("/findRoleAndMenu")
    public Result findRoleAndMenu(Integer id){
        Result result =new Result(true,"关联关系数据回显成功");
        try {
            List<Integer> menuIds=roleService.getRoleAndMenu(id);
            result.setData(menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"关联关系数据回显失败");
        }
        return result;
    }

    //修改角色信息以及与权限以及菜单的关联关系
    @RequestMapping("edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        Result result =new Result(true,"修改成功");
        try {
            roleService.edit(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
        return result;
    }

    //查询所有权限信息
    @RequestMapping("findAllPermission")
    public Result findAllPermission(){
        Result result = new Result(true, "获取权限成功");
        try {
            List<Permission> permissions = permissionService.findAll();
            result.setData(permissions);
        } catch (Exception e) {
            e.printStackTrace();
            new Result(false,"获取权限失败");
        }
        return result;
    }

    //查询所有菜单信息
    @RequestMapping("findAllMenu")
    public Result findAllMenu(){
        Result result = new Result(true, "获取菜单成功");
        try {
            List<Menu> menus = menuService.findAll();
            result.setData(menus);
        } catch (Exception e) {
            e.printStackTrace();
            new Result(false,"获取菜单失败");
        }
        return result;
    }
}
